package com.zmx.common.common.invocation;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.zmx.common.common.security.dto.OauthTokenDto;
import lombok.Data;


/**
 * @Description: 在父子线程或线程池中保证线程参数的正确传递
 * @ClassName: InvocationInfoProxy
 * @Author zhaomxr
 * @Date 2021-08-24 16:14
 */
@Data
public class InvocationInfoProxy {

    private static TransmittableThreadLocal<InvocationInfo> threadLocal = new TransmittableThreadLocal<InvocationInfo>() {
        @Override
        protected InvocationInfo initialValue() {
            return new InvocationInfo();
        }
    };

    /**
     * 取当前线程上下文信息
     *
     * @return
     */
    public static InvocationInfo get() {
        return threadLocal.get();
    }

    /**
     * 设置当前线程上下文信息
     *
     * @param invocationInfo
     */
    public static void set(InvocationInfo invocationInfo) {
        threadLocal.set(invocationInfo);
    }

    public static void setUserId(String userId) {
        threadLocal.get().setUserId(userId);
    }

    public static void setUserName(String userName) {
        threadLocal.get().setUserName(userName);
    }

    public static void setSystemId(String systemId) {
        threadLocal.get().setSystemId(systemId);
    }

    public static void setThreadName(String threadName) {
        threadLocal.get().setThreadName(threadName);
    }

    public static void setTocken(String tocken) {
        threadLocal.get().setTocken(tocken);
    }

    public static void setOauthTokenDto(OauthTokenDto oauthTokenDto) {
        threadLocal.get().setOauthTokenDto(oauthTokenDto);
    }

    public static String getUserId() {
        return threadLocal.get().getUserId();
    }

    public static String getUserName() {
        return threadLocal.get().getUserName();
    }

    public static String getSystemId() {
        return threadLocal.get().getSystemId();
    }

    public static String getThreadName() {
        return threadLocal.get().getThreadName();
    }

    public static String getTocken() {
        return threadLocal.get().getTocken();
    }

    public static OauthTokenDto getOauthTokenDto() {
        return threadLocal.get().getOauthTokenDto();
    }

    /**
     * 清空线程参数
     */
    public static void clear() {
        threadLocal.remove();
    }

}
